package uiComponents;


import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import cPanes.CPane;



/** 
 * ContextMenu provides the basic context menu functionality for all CPanes, and acts as an ActionListener for all menu items.
 * To add custom functionality, add a new JMenuItem, add the context menu as an ActionListener, and add the appropriate responses in the
 * inherited MenuActions() method.
 */
public class ContextMenu extends JPopupMenu implements ActionListener{

	private JMenuItem[] operations;
	private JMenuItem[] depthop;
	
	/**
	 * Creates a new ContextMenu. This automatically provides any pane with all the functionality found in OrgPane.
	 */
	public ContextMenu(){
		JMenu depth = new JMenu("Move");
		depthop = new JMenuItem[4];
		operations = new JMenuItem[3];
		operations[0] = new JMenuItem("Delete");
		operations[1] = new JMenuItem("Copy");
		operations[2] = new JMenuItem("Cut");
		depthop[0] = new JMenuItem("To Top");
		depthop[1] = new JMenuItem("Up");
		depthop[2] = new JMenuItem("Down");
		depthop[3] = new JMenuItem("To Bottom");
		for (int i=0; i<operations.length; i++){
			operations[i].addActionListener(this);
			this.add(operations[i]);
		}
		for (int i=0; i<depthop.length; i++){
			depthop[i].addActionListener(this);
			depth.add(depthop[i]);
		}
		this.add(depth);
		this.addSeparator();
	}
	
	
	
	public void actionPerformed(ActionEvent arg0) {
		CPane c = (CPane)this.getInvoker();
		MainWindow p = (MainWindow)c.getTopLevelAncestor(); 
		Object o = arg0.getSource();
		if(o.equals(operations[0])){
			c.getParent().remove(c);
			p.repaint();
		}
		else if(o.equals(operations[1])){
			p.getManager().toClipboard(c.getClone());
		}
		else if(o.equals(operations[2])){
			p.getManager().toClipboard(c.getClone());
			c.getParent().remove(c);
			p.repaint();
		}
		else if(o.equals(depthop[0])){
			c.moveToTop();
		}
		else if(o.equals(depthop[1])){
			c.moveUp();
		}
		else if(o.equals(depthop[2])){
			c.moveDown();
		}
		else if(o.equals(depthop[3])){
			c.moveToBottom();
		}
		else{
			c.menuActions(arg0);
		}
	}


}